/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Authentification.java
 *
 * Created on 17 mars 2009, 09:20:18
 */

package client;

import java.awt.Color;

/**
 *
 * @author mailleta
 */
public class Authentification_GUI extends javax.swing.JDialog {

    private java.awt.Frame parent;
    private Client client;
    
    /** Creates new form Authentification */
    public Authentification_GUI(java.awt.Frame parent, boolean modal , Client cl) {
        super(parent, modal);
        this.client = cl;
        this.parent = parent;
        initComponents();
        this.setLocationRelativeTo(this.getParent());
        setVisible(true);    
    }

   


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        login_label = new javax.swing.JLabel();
        login_textfield = new javax.swing.JTextField();
        password_label = new javax.swing.JLabel();
        inscription_label = new javax.swing.JLabel();
        annuler_button = new javax.swing.JButton();
        connection_button = new javax.swing.JButton();
        password_textfield = new javax.swing.JPasswordField();
        login_notify = new javax.swing.JLabel();
        login_notify_label = new javax.swing.JLabel();
        password_notify_label = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Veuillez vous identifier");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        login_label.setText("Login");

        password_label.setText("Password");

        inscription_label.setForeground(new java.awt.Color(13, 64, 240));
        inscription_label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        inscription_label.setText("Vous n'êtes pas inscrit ? inscrivez vous !");
        inscription_label.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                inscription_labelMouseClicked(evt);
            }
        });

        annuler_button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/client/icons/cancel.png"))); // NOI18N
        annuler_button.setText("Annuler");
        annuler_button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                ed(evt);
            }
        });
        annuler_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                annuler_buttonActionPerformed(evt);
            }
        });

        connection_button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/client/icons/tick.png"))); // NOI18N
        connection_button.setText("Connection");
        connection_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connection_buttonActionPerformed(evt);
            }
        });

        login_notify_label.setText(" ");

        password_notify_label.setText(" ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(password_notify_label, javax.swing.GroupLayout.DEFAULT_SIZE, 417, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(password_label, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(password_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(login_notify_label, javax.swing.GroupLayout.DEFAULT_SIZE, 417, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(annuler_button, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(connection_button, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(login_notify, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(login_label, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(login_textfield, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(inscription_label, javax.swing.GroupLayout.DEFAULT_SIZE, 417, Short.MAX_VALUE)))
                .addGap(18, 18, 18))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addComponent(login_notify)
                .addGap(180, 180, 180))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(login_label)
                    .addComponent(login_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(login_notify_label)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(password_label)
                    .addComponent(password_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(password_notify_label)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(inscription_label)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(connection_button, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(annuler_button, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ed
        this.dispose();
}//GEN-LAST:event_ed

    private void inscription_labelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_inscription_labelMouseClicked
        // TODO add your handling code here:
        this.dispose();
        Inscription_GUI inscription = new Inscription_GUI(parent, true , client);
        inscription = null;

    }//GEN-LAST:event_inscription_labelMouseClicked

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_formWindowClosing


    private void connection_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connection_buttonActionPerformed
        // TODO add your handling code here:
        String pseudo = login_textfield.getText();
        char[] password_chr = password_textfield.getPassword();
        String password = String.valueOf(password_chr);
        try {
            if(pseudo.isEmpty()) {
                login_notify_label.setForeground(Color.red);
                login_notify_label.setText("Veuillez saisir un login valide.");
            } else if(password.isEmpty()) {
                password_notify_label.setForeground(Color.red);
                password_notify_label.setText("Veuillez saisir un password valide.");
            } else {
                client.traiterConnectionChat(pseudo, password);
            }
        } catch (Exception ex) {
            new TraiteException(ex,"Probleme d'entrées/sorties");
        }
}//GEN-LAST:event_connection_buttonActionPerformed

    private void annuler_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_annuler_buttonActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_annuler_buttonActionPerformed

    /**
    * @param args the command line arguments
    */
    /*
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Authentification_GUI dialog = new Authentification_GUI(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton annuler_button;
    private javax.swing.JButton connection_button;
    private javax.swing.JLabel inscription_label;
    private javax.swing.JLabel login_label;
    private javax.swing.JLabel login_notify;
    private javax.swing.JLabel login_notify_label;
    private javax.swing.JTextField login_textfield;
    private javax.swing.JLabel password_label;
    private javax.swing.JLabel password_notify_label;
    private javax.swing.JPasswordField password_textfield;
    // End of variables declaration//GEN-END:variables

}
